﻿using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Text;

namespace Jinndev {

    /// <summary>
    /// Cocos节点
    /// </summary>
    public class CocosNode {

        /// <summary>原始数据</summary>
        public JObject obj;

        /// <summary>父节点</summary>
        public CocosNode parent;

        /// <summary>子节点</summary>
        public List<CocosNode> children = new();

        /// <summary>当前节点上的所有组件</summary>
        public List<CocosComponent> components = new();

        public CocosNode(JObject obj) {
            this.obj = obj;
        }

        public void AddChild(CocosNode node) {
            children.Add(node);
            node.parent = this;
        }

        public void AddComponent(CocosComponent com) {
            components.Add(com);
            com.node = this;
        }

        public string Name => obj["_name"].Value<string>();

        public string Path {
            get {
                StringBuilder builder = new(Name);

                var node = this;
                while (node.parent != null) {
                    node = node.parent;
                    builder.Insert(0, "/").Insert(0, node.Name);
                }
                return builder.ToString();
            }
        }

    }
}
